#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<time.h>
#include<libgen.h>
#include<dirent.h>
void dfs(char* dirName,int level){
    char name[1024];
    DIR* dir;
    if((dir=opendir(dirName))==NULL){
        printf("目录不存在\n");
        exit(-1);
    }else{
        if(level==0) printf(".\n");
        struct dirent *ptr;

        while((ptr=readdir(dir))!=NULL){

            if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0)
            continue;
            if(ptr->d_type==DT_REG){
                for(int i=0;i<level;++i) printf("\t");
                printf("|");
                for(int i=0;i<=level;++i) printf("__");
                printf(" %s\n",ptr->d_name);
            }else if(ptr->d_type==DT_DIR){
                if(strlen(dirName)+2+strlen(ptr->d_name)<sizeof(name)){
                    sprintf(name,"%s/%s",dirName,ptr->d_name);
                for(int i=0;i<level;++i) printf("\t");
                printf("|");
                for(int i=0;i<=level;++i) printf("__");
                    printf(" %s/\n",ptr->d_name);
                    dfs(name,level+1);
                }

            }
        }
        closedir(dir);
    }

}
int main(int args,char * argv[]){
    if(args==1){
        dfs(".",0);
        printf("\n");
    }else{
        dfs(argv[1],0);
         printf("\n");
    }
    return 0;
}
